package cfgs.gestudent.main;

import java.awt.EventQueue;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import cfgs.gestudent.controller.lector.FuncionsLectorRegistre;
import cfgs.gestudent.finestra.main.RegistrarAssistenciaLector;
import cfgs.gestudent.finestra.preferencies.FinestraPreferenciesServidorInici;
import cfgs.gestudent.models.EntitatSerivodBBDD;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;
import cfgs.gestudent.preferencies.PreferenciesServidor;

public class TestGestudentRegAlumn {

	public static void main(String[] args) {
		//TODO pensar que tens dos main (per alumnes i per administracio)
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConfiguracionsIdiomes conf = new ConfiguracionsIdiomes();
					ResourceBundle rb = conf.getIdioma();
					
					Connection connexio = null;
					PreferenciesServidor ps = new PreferenciesServidor();
					EntitatSerivodBBDD esbd = ps.getParametre();
					
					String server = esbd.getServidor();
					String port = esbd.getPort();
					String nameDB = esbd.getNomBD();
					String user = esbd.getUsuari();
					String password = esbd.getPassword();
					
					try {
						Class.forName("com.mysql.jdbc.Driver");
						connexio = DriverManager.getConnection("jdbc:mysql://"+server+":"+port+"/"+nameDB, user, password);
					} catch(SQLException e){
						JOptionPane.showMessageDialog(null, rb.getString("missatgeConexioIncorrecta"), rb.getString("titolConexioIncorrecta"), JOptionPane.ERROR_MESSAGE);
					}catch(ClassNotFoundException e){
						JOptionPane.showMessageDialog(null, rb.getString("missatgeErrorDriver"), rb.getString("titolErrorDriver"), JOptionPane.ERROR_MESSAGE);
					}
					
					
					if(connexio == null){
						String grFingerNativeDirectory = new File(".").getAbsolutePath();
						FuncionsLectorRegistre.setFingerprintSDKNativeDirectory(grFingerNativeDirectory);
						RegistrarAssistenciaLector fl = new RegistrarAssistenciaLector();
						
						FinestraPreferenciesServidorInici fpsi = new FinestraPreferenciesServidorInici();
						fpsi.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						fpsi.setVisible(true);
					}else{
						String grFingerNativeDirectory = new File(".").getAbsolutePath();
						FuncionsLectorRegistre.setFingerprintSDKNativeDirectory(grFingerNativeDirectory);
						RegistrarAssistenciaLector fl = new RegistrarAssistenciaLector();
					}
										
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}

}
